<template>
  <!-- 浇注批次表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="500px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120">
      <el-row>
        <el-col :span="24">
          <el-form-item label="产品" prop="productId">
            <common-product-select :formData="formData" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="铁水批次" prop="waterBatch">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.waterBatch"
              placeholder="请输入铁水批次"
              clearable
            />

            <span v-else>{{ formData.waterBatch }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="铁水材质" prop="waterMaterial">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.waterMaterial"
              placeholder="请输入铁水材质"
              clearable
            />

            <span v-else>{{ formData.waterMaterial }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="浇注模数" prop="pourModeNum">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.pourModeNum"
              placeholder="请输入浇注模数"
              clearable
            />

            <span v-else>{{ formData.pourModeNum }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="计划时间" prop="planTime">
            <el-date-picker
              v-if="!props.isReadonly"
              v-model="formData.planTime"
              type="datetime"
              placeholder="请选择计划时间"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="浇注工" prop="pourUser">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.pourUser"
              placeholder="请输入浇注工"
              clearable
            />

            <span v-else>{{ formData.pourUser }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="班次" prop="pourClass">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.pourClass"
              placeholder="请输入班次"
              clearable
            />

            <span v-else>{{ formData.pourClass }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import CommonProductSelect from "@/views/mes/lineBi/common/commonProductSelect.vue"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESBIPRODUCTLINEDATA6ADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  productId: [
    { required: true, message: "请选择产品", trigger: "blur" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  waterBatch: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
  waterMaterial: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
  pourModeNum: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
  planTime: [{ required: true, message: "请选择计划时间", trigger: "blur" }],
  pourUser: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  pourClass: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  productId: "",
  productCode: "",
  productName: "",
  waterBatch: "",
  waterMaterial: "",
  pourModeNum: "",
  planTime: "",
  pourUser: "",
  pourClass: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        productId,
        productCode,
        productName,
        waterBatch,
        waterMaterial,
        pourModeNum,
        planTime,
        pourUser,
        pourClass,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        productId,
        productCode,
        productName,
        waterBatch,
        waterMaterial,
        pourModeNum,
        planTime,
        pourUser,
        pourClass,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.productId = "";
  formData.productCode = "";
  formData.productName = "";
  formData.waterBatch = "";
  formData.waterMaterial = "";
  formData.pourModeNum = "";
  formData.planTime = "";
  formData.pourUser = "";
  formData.pourClass = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 浇注批次保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/biproductlinedata6/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
